package cn.hls.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import cn.hls.dao.CertSetDao;
import cn.hls.dao.MemberDao;
import cn.hls.entity.Member;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.MemberService;

/**
 * @fileName: MemberServiceImpl.java
 * @package: cn.christ.service.impl
 * @Description: 会友业务逻辑接口实现类
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-7-18
 */
@Service("memberService")
public class MemberServiceBean extends BaseServiceBean<Member> implements
		MemberService {
	@Resource(name = "memberDao")
	private MemberDao dao;
	@Resource(name="certSetDao")
	private CertSetDao certSetDao;

	@Override
	public boolean update(Member entity) throws Exception {
		Member tempEntity = dao.find(entity.getId());
		Date createTime = tempEntity.getCreateTime();
		BeanUtils.copyProperties(entity, tempEntity);
		tempEntity.setCreateTime(createTime);
		tempEntity.setUpdateTime(new Date());
		return super.update(tempEntity);
	}

	@Override
	public void delete(Object entityId) throws Exception {
		dao.delete(entityId);
	}

	@Override
	public PageInfo<Member> loadMemberInfo(Member entity,
			PageInfo<Member> page) throws Exception {
		return dao.loadMemberInfo(entity, page);
	}

	@Override
	public List<Member> loadMemberList(Member entity) throws Exception {
		return dao.loadMemberList(entity);
	}

	@Override
	public boolean exists(Member entity) throws Exception {
		return dao.exists(entity);
	}

	@Override
	public Map<String, Integer> loadExpiredMembers(Long companyId) throws Exception {
		return dao.loadExpiredMembers(companyId,certSetDao.loadcertSetInfoToMap(null));
	}

	@Override
	public List<Member> loadMemberOfExpired() throws Exception {
		return dao.loadMemberOfExpired(certSetDao.loadcertSetInfoToMap(null));
	}

	@Override
	public Map<Long, List<Member>> getMemberListToMap(List<Member> memberList)
			throws Exception {
		Map<Long,List<Member>>map=new HashMap<Long,List<Member>>();//定义map集合变量
		List<Member>tempListist=null;
		for(Member item:memberList){
			if(item.getCompany()!=null  ){//人员信息中的企业信息是否存在
				Long companyId=item.getCompany().getId();
				if(map.containsKey(companyId)){
					tempListist=map.get(companyId);
				}else{
					tempListist=new ArrayList<Member>();
				}
				tempListist.add(item);
				map.put(companyId, tempListist);
			}
		}
		return map;
	}


}
